package com.reliable.api.dto.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.reliable.api.dto.ip.taskInfo.IpIntelligenceDto;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备信息导出专用 DTO
 * 将复杂的 EquipmentDto 扁平化，以便 EasyExcel 导出。
 */
@Data
@HeadRowHeight(20)
@ContentRowHeight(18)
public class EquipmentExcel {

    @ExcelProperty("受害者ID")
    @ColumnWidth(40)
    private String victimId;

    @ExcelProperty("首次发现")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date firstSeen;

    @ExcelProperty("最后活动")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date lastSeen;

    @ExcelProperty("访问次数")
    @ColumnWidth(10)
    private Long visitCount;

    @ExcelProperty("当前在线")
    @ColumnWidth(10)
    private String online; // 转换为 "是" 或 "否"

    @ExcelProperty("域名数")
    @ColumnWidth(10)
    private Long domainCount;

    @ExcelProperty("最近域名")
    @ColumnWidth(30)
    private String latestDomain;

    @ExcelProperty("IP数")
    @ColumnWidth(10)
    private String ipCountSummary; // 汇总信息，例如 "4个IP"

    @ExcelProperty("IP详情")
    @ColumnWidth(60)
    private String ipDetails; // 将IP列表转换为字符串

    @ExcelProperty("最近IP")
    @ColumnWidth(15)
    private String latestIp;

    @ExcelProperty("最近IP归属地")
    @ColumnWidth(25)
    private String latestIpLocation;

    @ExcelProperty("操作系统")
    @ColumnWidth(20)
    private String os;

    @ExcelProperty("浏览器")
    @ColumnWidth(20)
    private String browser;

    @ExcelProperty("心跳数")
    @ColumnWidth(10)
    private Integer heartbeatNum;

    @ExcelProperty("封用户标志")
    @ColumnWidth(10)
    private String banVictimFlag; // 转换为 "是" 或 "否"

    // --- 以下是 transient 字段，EasyExcel 会忽略它们，仅用于内部计算 ---

    private transient Map<Integer, List<IpIntelligenceDto>> ipCount;

    // --- 自定义 Getter 方法，用于动态生成导出内容 ---

    public String getOnline() {
        // 使用Boolean.TRUE.equals避免NullPointerException
        return Boolean.TRUE.equals(this.online) ? "是" : "否";
    }

    public String getIpCountSummary() {
        if (this.ipCount == null || this.ipCount.isEmpty()) {
            return "0个IP";
        }
        int totalIpCount = this.ipCount.values().stream()
                .mapToInt(List::size)
                .sum();
        return totalIpCount + "个IP";
    }

    public String getIpDetails() {
        if (this.ipCount == null || this.ipCount.isEmpty()) {
            return "";
        }
        return this.ipCount.values().stream()
                .flatMap(List::stream)
                .map(this::formatIpIntelligence)
                .collect(Collectors.joining("; "));
    }

    private String formatIpIntelligence(IpIntelligenceDto dto) {
        if (dto == null) return "";
        return String.format("IP:%s, 位置:%s, 任务数:%d, 首次:%s, 最后:%s",
                dto.getIp(),
                dto.getIpLocation(),
                dto.getTaskCount(),
                dto.getFirstSeen() != null ? dto.getFirstSeen().toString() : "",
                dto.getLastSeen() != null ? dto.getLastSeen().toString() : "");
    }

    public String getBanVictimFlag() {
        if (this.banVictimFlag != null && this.banVictimFlag.equals("1")) {
            return "是";
        }
        return "否";
    }
}